<?php
/*
	插件概览
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
set_html('title','登录系统');
if($_RQ['op']=='get'){
	$return['set']=$this->plugin_setting('setting');
	$return['set']['loginlogo']=tomedia($return['set']['loginlogo']);
	$return['set']['loginbg']=tomedia($return['set']['loginbg']);
	if($_SESSION['os']=='windows'){
		$return['session_id']=session_id();
	}
	$_SESSION['logout_forward']='jy_kexie/login/';
	$_SESSION['forward_url']='jy_kexie/index/'; 
	$return['forward_url']=base64_encode($_SESSION['forward_url']);
	$return['info']=array('role_id'=>'2');
	$return['roles_type']=$this->roles_type;
	if($_SESSION['uid']){
		unset($_SESSION['uid']);
		unset($_SESSION['role_id']);
		unset($_SESSION['member']);
		exi('','',custom_url('jy_kexie/login/',true));
	}
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	if(!$post['password'] || !$post['username']){
		exi('请填写账户名和密码','error');
	}
	if($post['role_id']==0){
		$tablename='jy_kexie_user';
	}
	if($post['role_id']==1){
		$tablename='jy_kexie_area_user';
	}
	if($post['role_id']==2){
		$tablename='jy_kexie_store';
	}
	if($post['role_id']==3){
		$tablename='jy_kexie_worker';
	}
	if($post['role_id']==4){
		$tablename='jy_kexie_project_user';
	}
	if($post['role_id']==5){
		$tablename='jy_kexie_project_cuser';
	}
	$user=pdo_get($tablename,array('uniacid'=>$_SESSION['uniacid'],'username'=>$post['username']));
	if(!$user || $post['password']!=base64_decode($user['password'])){
		exi('用户名或密码错误','error');
	}
	if(!$user['mid']){
		do{
			$username='user_'.random(10);
		}while(pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'username'=>$username),'id'));
		$member=array('username'=>$username,'password'=>trim($post['password']));
		$uid=model('core/member')->register($member);
		pdo_update($tablename,array('mid'=>$uid),array('uniacid'=>$_SESSION['uniacid'],'id'=>$user['id']));
		//exi('该机构没有绑定用户/管理员！','error');
	}
	$_SESSION['uid']=$user['mid']?:$uid;
	$_SESSION['role_id']=$post['role_id'];
	$_SESSION['member']['username']=$user['username'];
	//绝对地址，会刷新页面
	if($post['role_id']==0){
		$url='/index/';
	}
	if($post['role_id']==1){
		$url='/area.index/';
	}
	if($post['role_id']==2){
		$url='/store.index/';
	}
	if($post['role_id']==3){
		$url='/worker.index/';
	}
	if($post['role_id']==4){
		$url='/project.index/';
	}
	if($post['role_id']==5){
		$url='/gongsi.index/';
	}
	exi('登陆成功!','',custom_url($url,true));
	
}